

module.exports = function ($scope, $state, $resource, $modal, toaster ){

    
    app.directive('contenteditable', function() {
        return {
          require: 'ngModel',
          link: function(scope, element, attrs, ctrl) {
         
            element.bind('keyup', function() {
              scope.$apply(function() {
                var html=element.html();
      
                ctrl.$setViewValue(html);
              });
            });
      
          }
        };
      });

      //题库列表
    $scope.search = function (pageNo){
        var para = {
            'pageNo': pageNo || $scope.currentPage,
            'pageSize': $scope.itemsPerPage,
            'title': $scope.info.title,
            'type': $scope.info.type
            
        }
        $resource('/api/ac/goc/counselingService/findWarehouseList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.list = res.data.results;
                $scope.totalItems = res.data.totalRecord;
                $scope.currentPage = pageNo;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    //笔记列表
    $scope.searchnote = function (pageNo){
        var para = {
            'pageNo': pageNo || $scope.notecurrentPage,
            'pageSize': $scope.noteitemsPerPage
        }
        $resource('/api/ac/goc/counselingService/findMyNoteList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.notelist = res.data.results;
                $scope.notetotalItems = res.data.totalRecord;
                $scope.notecurrentPage = pageNo;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    //查询type列表
    $scope.searchtype = function (){
        var para = {
            'type': '4',
            'state': '1'
        }
        $resource('/api/as/goc/pelawinfo/findTypeList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.typelist = res.data.results;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }
   


    init();
    //初始化列表
    function init(){
        $scope.savenote_show = false;
        $scope.findnote_show = true;
        $scope.currentPage = 1;		//当前页码
        $scope.itemsPerPage = 10;	//每页显示几条
        $scope.notecurrentPage = 1;		//当前页码
        $scope.noteitemsPerPage = 5;	//每页显示几条
        $scope.searchform = {};
        $scope.search(1);
        $scope.rulesinfo = {};
        $scope.searchtype();
        $scope.searchnote();
    }

    //添加or修改
    $scope.saveWarehouseInfo = function(info,change_type) {
        info.openstate = false;
        var modalInstance = $modal.open({
            template: require('../views/saveWarehouseInfo.html'),
            controller: 'saveWarehouseInfo',
            size: 'lg',
            resolve: {
                item : function () {
                    return info;
                },
                flag : function () {
                    return info == 1;
                },
                change_type : function () {
                    return change_type;//0管理员直接修改题库   1客服申请修改题库    2修改我的笔记
                }
            }
        });
        //关闭模态框刷新页面
        modalInstance.result.then(function() {
            init();
        });
    }

    //添加log
    $scope.savelog = function (info,type){
        var para = {
            'id': info.id,
            'type': type
        }
        console.log(para)
        $resource('/api/ac/goc/counselingService/insertworklog', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.typelist = res.data;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    //查看
    $scope.gatRulesInfo = function(info,type) {
        $scope.savelog(info,type);
        info.openstate = true;
        var modalInstance = $modal.open({
            template: require('../views/saveWarehouseInfo.html'),
            controller: 'saveWarehouseInfo',
            size: 'lg',
            resolve: {
                item : function () {
                    return info;
                },
                flag : function () {
                    return info == 1;
                },
                change_type : function () {
                    return '3';//0管理员直接修改题库   1客服申请修改题库    2修改我的笔记 3 查看
                }
            }
        });
        //关闭模态框刷新页面
        modalInstance.result.then(function() {
            info.openstate = 'false';
            init();
        });
    }


    //开启关闭新建笔记
    $scope.opennote = function() {
        $scope.savenote_show = !$scope.savenote_show ;
    }
    
    //关闭新建笔记
    $scope.cancel = function() {
        $scope.savenote_show = false;
        $scope.rulesinfo = {};
    }

    //开启关闭我的笔记
    $scope.opennotelist = function() {
        $scope.searchtype();
        $scope.searchnote();
        $scope.findnote_show = !$scope.findnote_show ;
    }
    
    //保存
    $scope.save = function (){
        console.log($scope.rulesinfo)
        if($scope.rulesinfo.type == undefined){
            toaster.error({title: '', body: '请选择类别！'});
          return;
        }
        if($scope.rulesinfo.info == undefined){
            toaster.error({title: '', body: '请输入详情！'});
          return;
        }
        if($scope.rulesinfo.title == undefined){
            toaster.error({title: '', body: '请输入标题！'});
          return;
        }
        var para = {
            'title': $scope.rulesinfo.title,
            'info': $scope.rulesinfo.info,
            'type': $scope.rulesinfo.type,
            'id' :  $scope.rulesinfo.id
        }
        $resource('/api/ac/goc/counselingService/saveNote', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                toaster.success({title: '', body: '保存成功'});
                $scope.cancel();
                init();
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }


};